﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSMOTREPP: ObservableObject
    {
        #region Campos
        
        private string codmot_mrp, nommot_mrp;
        private Int32 premed_mrp, prebog_mrp, costo_mrp;

        
        MDOgeneral mod = new MDOgeneral();
        #endregion

        #region Propiedades
        
        public string CODMOT_MRP
        {
            get { return codmot_mrp; }
            set { codmot_mrp = value; }
        }

        public string NOMMOT_MRP
        {
            get { return nommot_mrp; }
            set { nommot_mrp = value; }
        }

        public Int32 PREMED_MRP
        {
            get { return premed_mrp; }
            set { premed_mrp = value; }
        }

        public Int32 PREBOG_MRP
        {
            get { return prebog_mrp; }
            set { prebog_mrp = value; }
        }

        public Int32 COSTO_MRP
        {
            get { return costo_mrp; }
            set { costo_mrp = value; }
        }

        private Boolean _EXISTE = false;
        public Boolean EXISTE
        {
            get { return _EXISTE; }
            set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
        }
        #endregion


        #region Metodos

        public List<CLSMOTREPP> listarReparaciones()
        {

            try
            {
                string query = "select codmot_mrp, nommot_mrp, PREMED_MRP, PREBOG_MRP, COSTO_MRP from motrepp order by nommot_mrp";
                //string query = "select codmot_mrp, nommot_mrp, to_number(nvl(PREMED_MRP,0)) as PREMED_MRP, to_number(nvl(PREBOG_MRP,0)) as PREBOG_MRP, to_number(nvl(COSTO_MRP,0)) as COSTO_MRP from motrepp order by nommot_mrp";
                DataTable val = mod.consulta(query);
                List<CLSMOTREPP> lisrep = new List<CLSMOTREPP>();
                foreach (DataRow r in val.Rows)
                {
                    lisrep.Add(new CLSMOTREPP
                    {
                        CODMOT_MRP = r[0].ToString(),
                        NOMMOT_MRP = r[1].ToString(),
                        PREMED_MRP = Convert.ToInt32(r[2]),
                        PREBOG_MRP = Convert.ToInt32(r[3]),
                        COSTO_MRP = Convert.ToInt32(r[4])
                    });
                }
                return lisrep;
            }
            catch (Exception ex) { throw (ex); }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select codmot_mrp, nommot_mrp, PREMED_MRP, PREBOG_MRP, COSTO_MRP from motrepp where codmot_mrp='" + CODMOT_MRP + "' order by nommot_mrp ";
                DataTable dt = mod.consulta(query);
                if (dt.Rows.Count > 0)
                {
                    this.CODMOT_MRP = dt.Rows[0]["CODMOT_MRP"].ToString();
                    this.NOMMOT_MRP = dt.Rows[0]["NOMMOT_MRP"].ToString();
                    this.PREMED_MRP = Convert.ToInt32(dt.Rows[0]["PREMED_MRP"]);
                    this.PREBOG_MRP = Convert.ToInt32(dt.Rows[0]["PREBOG_MRP"]);
                    this.COSTO_MRP = Convert.ToInt32(dt.Rows[0]["PREBOG_MRP"]);

                    this.EXISTE = true;
                }
                else
                {
                    this.NOMMOT_MRP = "";
                    this.EXISTE = false;
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable buscar()
        {
            try
            {
                string query = "select codmot_mrp as Codigo, nommot_mrp as Nombre from  motrepp where ";

                if (NOMMOT_MRP != "")
                {
                    query = query + "UPPER(NOMMOT_MRP) like '%" + NOMMOT_MRP.ToUpper() + "%' and ";
                }
                query = query.Remove(query.Length - 4).Trim() + " Order by NOMMOT_MRP asc";
                return mod.consulta(query);
            }
            catch (Exception e) { throw e; }
        }

        public Boolean existe()
        {
            try
            {
                string query = "select coalesce(count(CODMOT_MRP),0) from motrepp where (CODMOT_MRP='" + CODMOT_MRP + "')";
                DataTable val = mod.consulta(query);
                if (val.Rows[0][0].ToString().Equals("0"))
                {
                    return false;
                }
                return true;
            }
            catch (Exception e) { throw e; }
        }

        #endregion
        
    }
}

